package com.aptemo.webetek.client.widgets.betslip;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EEditMode;
import com.aptemo.webetek.client.widgets.CurrencyWidget;
import com.aptemo.webetek.client.widgets.NumberWidget;
import com.aptemo.webetek.shared.PickBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasAllKeyHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class BetSlip
		extends Composite implements BetSlipPresenter.Display
{
	private static BetSlipUiBinder	uiBinder	= GWT.create(BetSlipUiBinder.class);

	interface BetSlipUiBinder extends UiBinder<Widget, BetSlip>
	{
	}

	@UiField
	Button			submit;

	@UiField
	TextBox			stakePerBet;

	@UiField
	CurrencyWidget	totalAmountField;

	@UiField
	NumberWidget	totalOdds;

	@UiField
	CurrencyWidget	possibleWinnings;

	@UiField
	Label			infoField;

	@UiField
	VerticalPanel	betsPanel;

	@UiField
	HTMLPanel		summaryPanel;

	//
	@UiField
	HTMLPanel		summaryPanelFinal;

	@UiField
	TextBox			totalAmountFinal;

	@UiField
	NumberWidget	totalOddsFinal;

	@UiField
	CurrencyWidget	earningsField;

	public BetSlip()
	{
		initWidget(uiBinder.createAndBindUi(this));

		// set info...
		infoField.setText(Webetek.getConstants().betSlipEntryText());
	}

	@Override
	public double getAmount()
	{
		double tmp = Double.parseDouble(stakePerBet.getText());
		return tmp;
	}

	@Override
	public HasAllKeyHandlers getStakePerBetKeyHandler()
	{
		return stakePerBet;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasValue<Double> getPossibleWinnings()
	{
		return possibleWinnings;
	}

	@Override
	public HasValue<Double> getEarningsField()
	{
		return earningsField;
	}

	@Override
	public HasValue<String> getStakePerBet()
	{
		return stakePerBet;
	}

	@Override
	public HasValue<String> getTotalAmountFinal()
	{
		return totalAmountFinal;
	}

	@Override
	public HasClickHandlers getSubmitButton()
	{
		return submit;
	}

	@Override
	public HasValue<Double> getTotalOdds()
	{
		return totalOdds;
	}

	@Override
	public HasValue<Double> getTotalOddsFinal()
	{
		return totalOddsFinal;
	}

	@Override
	public void renderList(List<PickBean> list, BetSlipPresenter handler)
	{
		betsPanel.clear();
		for (PickBean pick : list)
		{
			// addRow(pick);
			BetSlipRow row = new BetSlipRow(pick);
			betsPanel.add(row);

			row.addDeletePickHandler(handler);
		}

		if (list.size() > 0)
		{
			// summaryPanel.setVisible(true);
			betsPanel.setVisible(true);
		}
		else
		{
			// set info...
			infoField.setText(Webetek.getConstants().betSlipEntryText());

			summaryPanel.setVisible(false);
			summaryPanelFinal.setVisible(false);
			betsPanel.setVisible(false);
		}
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public void setDisplayMode(EEditMode mode)
	{
		if (mode == EEditMode.EREADONLY)
		{
			submit.setEnabled(false);
			stakePerBet.setReadOnly(true);
		}
		else
		{
			submit.setEnabled(true);
			stakePerBet.setReadOnly(false);
		}
	}

	@Override
	public void switchToFinalMode(boolean value)
	{
		if (value)
		{
			summaryPanel.setVisible(false);
			summaryPanelFinal.setVisible(true);
		}
		else
		{
			summaryPanel.setVisible(true);
			summaryPanelFinal.setVisible(false);
		}
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}
	
	@Override 
	public HasValue<Double> getTotalAmountField()
	{
		return totalAmountField;
	}
}
